package simulateOS.entity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class Folder {
    private String folderName;/*记录文件名*/
    private String type;/*记录文件的类型*/
    private int diskNum;/*记录当前文件夹的磁盘盘块号*/
    private String location;/*记录文件夹的绝对地址信息*/
    private double size;/*记录文件夹大小*/
    private String space;/*记录文件夹的大小显示0.0字节*/
    private Date createTime;/*记录文件夹创建时间*/
    private Folder parent;/*记录当前文件夹的父文件夹（若为根目录则置为null）*/
    private List<Object> children;/*后续保存当前目录下的子文件或者子目录*/
    private Path path;/*记录文件的地址信息类*/
    private int catalogMaxNum;/*记录当前目录下子目录的数量，以限制可创建的文件夹数量*/
    private int sumCreate;/*记录当前文件内曾经创建过的文件数量*/


    public Folder(String folderName,String location,int diskNum,Folder parent){
        this.folderName = folderName;
        this.location = location;
        this.diskNum = diskNum;
        this.parent = parent;
        this.size = 0.0D;
        this.space = this.size + "B";
        this.createTime = new Date();
        this.type = "文件夹";
        this.setChildren(new ArrayList<>());
        this.catalogMaxNum = 0;
        this.sumCreate = 1;
    }

    public String getFolderName() {
        return folderName;
    }

    public void setFolderName(String folderName) {
        this.folderName = folderName;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getDiskNum() {
        return diskNum;
    }

    public void setDiskNum(int diskNum) {
        this.diskNum = diskNum;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public double getSize() {
        return size;
    }

    public void setSize(double size) {
        this.size = size;
        this.setSpace(this.size + "B");
    }

    public String getSpace() {
        return space;
    }

    public void setSpace(String space) {
        this.space = space;
    }

    public String getCreateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        return format.format(this.createTime);
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Folder getParent() {
        return parent;
    }

    public void setParent(Folder parent) {
        this.parent = parent;
    }

    public List<Object> getChildren() {
        return children;
    }

    public void setChildren(List<Object> children) {
        this.children = children;
    }

    public Path getPath() {
        return path;
    }

    public void setPath(Path path) {
        this.path = path;
    }

    public int getCatalogMaxNum() {
        return catalogMaxNum;
    }

    public void setCatalogMaxNum(int catalogMaxNum) {
        this.catalogMaxNum = catalogMaxNum;
    }

    public void addChildren(Object child) {
        this.children.add(child);
    }

    public void removeChildren(Object child) {
        this.children.remove(child);
    }

    public boolean hasParent(){
        return this.parent != null;
    }

    public boolean hasChildren(){
        return !this.children.isEmpty();
    }

    public double getFolderSize(Folder folder){
        List<Object> children = folder.getChildren();
        double size = 0.0D;
        Iterator var4 = children.iterator();

        while(((Iterator<?>) var4).hasNext()) {
            Object child = var4.next();
            if (child instanceof File) {
                size += ((File)child).getSize();
            } else {
                size += getFolderSize((Folder)child);
            }
        }
        return Double.parseDouble(String.valueOf(size));
    }


    //命名查重（有问题）
    public boolean hasSameName(String newName){
        List<String> list = new ArrayList<>();
        if(hasChildren()){
            int length = getChildren().size(),i;
            for(i = 0;i < length;i++){
                if(getChildren().get(i) instanceof Folder){
                    list.add(((Folder)(getChildren().get(i))).getFolderName());

                }else if(getChildren().get(i) instanceof File){
                    list.add(((File)(getChildren().get(i))).getFileName());
                }
            }
            for(i = 0;i < length;i++){
                if(list.get(i).equals(newName)){
                    return true;
                }
            }
        }
        return false;
    }

    public int getSumCreate() {
        return sumCreate;
    }

    public void setSumCreate(int sumCreate) {
        this.sumCreate = sumCreate;
    }

    public String toString(){
        return getPath().getPathName();
    }

    public String toName(){
        return folderName;
    }
}
